/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hualu.zks.httpclient;

import com.hualu.lgproject.Model.File;
import com.hualu.zks.Dao.NodeDao;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author Ma Yong
 */
public class Delete_Rename {

    /**
     * @throws java.io.IOException
     */
    public StringBuilder res = null;
    private HttpResponse response;
    private DefaultHttpClient httpclient = new DefaultHttpClient();
    public static NodeDao nodeDao;
    public static GetFileList getfilelist;
    //public static File file;

    @SuppressWarnings("empty-statement")
    //public ArrayList<File> Delete(String URL, String IDtoDelete, int Nodeid) throws IOException {
    //public void Delete(String URL, String IDtoDelete, int Nodeid) throws IOException {
    //删除成功0；删除失败-1；其它错误-2；
     public int Delete(AsyncHTTPCallback callback,String URL, File file) throws IOException {
         
//        httpclient.getCredentialsProvider().setCredentials(new AuthScope("127.0.0.1", 8888), new UsernamePasswordCredentials("", ""));
//        HttpHost proxy = new HttpHost("127.0.0.1", 8888);
//        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
//        设置代理结束
        String LoginURL = "http://" + URL + "/WAN/ttl_delq.cgi";
        HttpPost httpost = new HttpPost(LoginURL);
//        ArrayList<File> filelist = new ArrayList<>();
//        filelist = getfilelist.getAllList(URL, Nodeid);
        String cTTLINFO = file.cTTLINFO();

            List<BasicNameValuePair> nvps = new ArrayList<>();//设置协议信息
//            nvps.add(new BasicNameValuePair("cCMD_ExEC.x", "100"));
//            nvps.add(new BasicNameValuePair("cPARENTID", "/01/01/"));
//            nvps.add(new BasicNameValuePair("SID", "12345678"));
//            nvps.add(new BasicNameValuePair("cCMD_EXEC.y", "100"));
//            //nvps.add(new BasicNameValuePair("Host", "192.168.1.110"));
//            nvps.add(new BasicNameValuePair("Host", URL));
//            //nvps.add(new BasicNameValuePair("cTTLID", IDtoDelete));
//            nvps.add(new BasicNameValuePair("cTTLID", file.getFileId()));
//            System.out.println(file.getFileId());
//            nvps.add(new BasicNameValuePair("cTTLINFO", cTTLINFO));
            
            ///
            nvps.add(new BasicNameValuePair("Host", URL));
            nvps.add(new BasicNameValuePair("SID", "92345679"));
            nvps.add(new BasicNameValuePair("cCMD_ExEC.x", "100"));
            nvps.add(new BasicNameValuePair("cCMD_EXEC.y", "100"));
            nvps.add(new BasicNameValuePair("cPARENTID", "/01/01/"));
            nvps.add(new BasicNameValuePair("cTTLID", file.getFileId()));
            nvps.add(new BasicNameValuePair("cCTYPE", "01"));
            nvps.add(new BasicNameValuePair("cBROAD", "FFFF"));
            nvps.add(new BasicNameValuePair("cTUSRC", "4"));
            nvps.add(new BasicNameValuePair("cTTLINFO", cTTLINFO));
                ///

            try {
//            // 设置请求的数据   
                httpost.setEntity(new UrlEncodedFormEntity(nvps, "SJIS"));
                httpost.setHeader("User-Agent", "MEI-LAN-REMOTE-CALL");
                response = httpclient.execute(httpost);
                return callback.onComplete(response);
                //获取响应信息
//                HttpEntity entity = response.getEntity();
//                InputStreamReader rr = new InputStreamReader(entity.getContent(), "UTF-8");
//                BufferedReader reader = new BufferedReader(rr);
//                StringBuilder builder = new StringBuilder();
//                System.out.println("StatusCode:"+response.getStatusLine().getStatusCode());//打印状态
//                rr.close();
            } catch (Exception e) {
                e.printStackTrace();
                //return false;
            } finally {
                httpost.abort();
            }
        return -2;
        } 

    public void Rename(AsyncHTTPCallback callback,String URL, File file, String IDEdited) throws IOException {

//        httpclient.getCredentialsProvider().setCredentials(new AuthScope("127.0.0.1", 8888), new UsernamePasswordCredentials("", ""));
//        HttpHost proxy = new HttpHost("127.0.0.1", 8888);
//        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
//        设置代理结束
        
        String LoginURL = "http://" + URL + "/WAN/ttl_neditq.cgi";
        HttpPost httpost = new HttpPost(LoginURL);
//        ArrayList<File> filelist = new ArrayList<>();
//        filelist = getfilelist.getAllList(URL, Nodeid);
        String cTTLINFO = file.cTTLINFO();

            List<BasicNameValuePair> nvps = new ArrayList<>();//设置协议信息
            nvps.add(new BasicNameValuePair("cCMD_ExEC.x", "100"));
            nvps.add(new BasicNameValuePair("cPARENTID", "/01/01/"));
            nvps.add(new BasicNameValuePair("SID", "12345678"));
            nvps.add(new BasicNameValuePair("cCMD_EXEC.y", "100"));
            //nvps.add(new BasicNameValuePair("Host", "192.168.1.110"));
            nvps.add(new BasicNameValuePair("Host", URL));
            //nvps.add(new BasicNameValuePair("User-Agent", "MEI-LAN-REMOTE-CALL"));
            nvps.add(new BasicNameValuePair("cTTLID", file.getFileId()));
            nvps.add(new BasicNameValuePair("cNAME", IDEdited));//编辑后的名字
            nvps.add(new BasicNameValuePair("cCTYPE", "01"));
            nvps.add(new BasicNameValuePair("cTTLINFO", cTTLINFO));

            try {
//            // 设置请求的数据   
                httpost.setEntity(new UrlEncodedFormEntity(nvps, "SJIS"));
                httpost.setHeader("User-Agent", "MEI-LAN-REMOTE-CALL");
                response = httpclient.execute(httpost);
                System.out.println("onComplete return code:"+callback.onComplete(response));
//                //获取响应信息
//                HttpEntity entity = response.getEntity();
//                InputStreamReader rr = new InputStreamReader(entity.getContent(), "UTF-8");
//                BufferedReader reader = new BufferedReader(rr);
//                StringBuilder builder = new StringBuilder();

//                for (String line = reader.readLine(); line != null; line = reader
//                        .readLine()) {
//        }
//                rr.close();
            } catch (Exception e) {
                e.printStackTrace();
                //return false;
            } finally {
                httpost.abort();
            }
        }

    public void deleteMultiFile(AsyncHTTPCallback callback, String URL, ArrayList<File> checkfilelist) throws IOException {
        for (int i = 0; i < checkfilelist.size(); i++) {
            if (Delete(callback, URL, checkfilelist.get(i)) != 0&&i<(checkfilelist.size()-1)) {
                int ret = JOptionPane.showConfirmDialog(null, "是否跳过\""+checkfilelist.get(i).getName()+"\"文件继续进行删除操作？", "提示", JOptionPane.OK_CANCEL_OPTION);
                if (ret != 0) {
                    break;
                }
            }
        }
    }

}
